import axios from 'axios'
import router from '../router/index'
import { Notify, Toast } from 'vant';

// 环境的切换
if (process.env.NODE_ENV == 'development') {
    // axios.defaults.baseURL = 'http://192.168.125.115:4000';
    axios.defaults.baseURL = 'http://localhost:4000';
}
else if (process.env.NODE_ENV == 'debug') {
    axios.defaults.baseURL = 'http://localhost:4000';
}
else if (process.env.NODE_ENV == 'production') {
    axios.defaults.baseURL = 'http://118.178.235.143:4000';
}
axios.defaults.timeout = 10000;
// 跨域请求可以携带Cookie
axios.defaults.withCredentials = true
// 默认请求方式
// axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest';
//请求头上面的token
axios.defaults.headers['token'] = sessionStorage.getItem('token') || ''

axios.defaults.headers.post['Content-Type'] = 'application/json'

axios.create({
    baseURL: global.GOLBAL_BASEURL,
    withCredentials: true
})

axios.interceptors.response.use(res => {
    if (Object.prototype.toString.call(res.data) != "[object Object]") {
        Notify({ type: 'danger', message: '服务器异常！' });
        return res.data
    }
    if (!res.success) {        // 状态码
        if (res.data.status == '401') {
            sessionStorage.removeItem('token');
            localStorage.removeItem('token')
        }
        return res.data
    }
    res.data.msg && Toast(res.data.msg);
    return res.data
}, (err) => {
    console.log('err', err);
})
export default axios